package com.frejoys.common.jsonserializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * 将BigDecimal类型数字去除后缀0并且转换为字符串
 */
public class BigDecimalSerializer extends StdSerializer<BigDecimal> {

    public static final BigDecimalSerializer instance = new BigDecimalSerializer();

    public BigDecimalSerializer() {
        super(BigDecimal.class);
    }

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(bigDecimal.stripTrailingZeros().toPlainString());
    }

}
